package com.zbc.controller.admin;

import com.zbc.constant.JwtClaimsConstant;
import com.zbc.context.BaseContext;
import com.zbc.dto.EmployeeDTO;
import com.zbc.dto.EmployeeLoginDTO;
import com.zbc.dto.EmployeePageDTO;
import com.zbc.entity.Employee;
import com.zbc.properties.JwtProperties;
import com.zbc.result.PageResult;
import com.zbc.result.Result;
import com.zbc.service.EmployeeService;
import com.zbc.utils.JwtUtil;
import com.zbc.vo.AllPageVo;
import com.zbc.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Api(tags = "员工相关接口")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        Employee employee = this.employeeService.login(employeeLoginDTO);

        Map<String,Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                this.jwtProperties.getAdminSecretKey(),
                this.jwtProperties.getAdminTtl(),
                claims
        );

        BaseContext.setCurrentId(employee.getId());

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    @PostMapping
    @ApiOperation("添加员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("添加员工,{}",employeeDTO);
        employeeService.insertEmployee(employeeDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("员工分页")
    public Result<PageResult> page(EmployeePageDTO employeePageDTO){
        PageResult result = employeeService.pageQuery(employeePageDTO);
        return Result.success(result);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("切换员工状态")
    public Result updateStatus(@PathVariable Integer status,Integer id){
        employeeService.updateEmployeeStatus(status,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("获取员工详细信息")
    public Result<Employee> getEmployeeById(@PathVariable Integer id){
        Employee employee=employeeService.getEmployeeById(id);
        return Result.success(employee);
    }

    @PutMapping
    @ApiOperation("修改员工信息")
    public Result updateEmployee(@RequestBody Employee employee){
        employeeService.updateEmployee(employee);
        return Result.success();
    }
}
